package room_dao

import (
	"hft/Database/admin"
	"hft/global"
	"hft/test"
	"hft/test/mi/loupan_swiper/loupan_swiper_utils"
	"hft/test/mi/room/room_model"
	"hft/test/mi/room/room_utils"
	"math"
	"strings"
)

func MiS() {
	var list []*admin.LouPan
	global.OC_DB.Find(&list)
	MiRoom(list)
}
func MiRoom(list []*admin.LouPan) {
	var a1 = make([]*room_model.OldRoom, 0)
	var a2 = make([]*room_model.OldRoom, 0)
	var a3 = make([]*room_model.OldRoom, 0)
	test.Sql().Table("flw_apart").Find(&a1)
	test.Sql().Table("flw_apart2").Find(&a2)
	a3 = append(a3, a1...)
	a3 = append(a3, a2...)
	var roomList = make([]*admin.Room, 0)
	for _, v := range a3 {
		if loupan_swiper_utils.IsContainThisLou(list, uint(v.PID)) {
			// 获取总价
			TotalPrice, _ := room_utils.ExtractFirstNumber(v.Money)
			ShouFu, _ := room_utils.ExtractFirstNumber(v.Sf)
			// 获取首付几成
			firstPay := math.Round(float64(ShouFu) / float64(TotalPrice) * 10)
			// 获取面积
			area, _ := room_utils.ExtractFirstNumber(v.Meas)
			// 获取利率类型
			var lilvType int = 0
			if strings.Contains(v.Hcate, "商") {
				lilvType = 1
			}
			bath := room_utils.ExtractDigitsBeforeSubstring(v.Hx, "卫")
			ting := room_utils.ExtractDigitsBeforeSubstring(v.Hx, "厅")
			pro := room_utils.ConfirmPro(v.Hcate)
			// 朝向
			forward := room_utils.JustOrie(v.Orie)
			// 销售状态
			var status int
			if res := room_utils.FindLou(list, uint(v.PID)); res != nil {
				status = res.Status
			}
			var referencePrice = 0
			if area != 0 {
				referencePrice = TotalPrice * 10000 / area
			}
			var item = &admin.Room{
				Name:           v.Hx,
				Grounding:      true,
				FirstPay:       int(firstPay),
				Area:           area,
				LouPanID:       v.PID,
				RoomPayType:    lilvType,
				BathRoom:       bath,
				Office:         ting,
				ReferencePrice: referencePrice,
				PropertyUse:    pro,
				Cover:          v.Hpic,
				Forward:        forward,
				Status:         status,
				RoomId:         v.Type,
			}
			roomList = append(roomList, item)
		}

	}
	global.OC_DB.Create(&roomList)

}
